/*
 *
 * Copyright 2011, Ibrahim Arief
 * 
 * This file is part of Bambu Game Backend.
 *
 * Bambu Game Backend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bambu Game Backend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Bambu Game Backend.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package com.appspot.bambugame.server.rest;

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.bambugame.server.data.HangmanQuestion;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class HangmanSentencesBatchServlet extends HttpServlet
{
    /**
     * 
     */
    private static final long serialVersionUID = -8957362320144318364L;

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            writeInputForm( req, resp.getWriter() );
            
            String tCommandString = req.getParameter( "cmd" );
            
            if (tCommandString != null && tCommandString.equalsIgnoreCase( "add" ))
            {
                String tBatchInput = req.getParameter( "batchinput" );
                
                if (tBatchInput != null)
                {
                    tBatchInput = tBatchInput.trim().toLowerCase();
                    if (tBatchInput.isEmpty()) return;
                    
                    String[] tSentenceHintInputArray = tBatchInput.split( "\\|" );
                    
                    for (String tSentenceHintString : tSentenceHintInputArray)
                    {
                        String[] tWordArray = tSentenceHintString.split( "@" );
                        
                        if (tWordArray.length != 2) 
                        {
                            resp.getWriter().write("\n<br>Cannot insert [" + tSentenceHintString + "], invalid format.");
                            continue;
                        }
                        
                        String tSentence = tWordArray[0];
                        String tHint = tWordArray[1];
                        
                        if (tSentence.length() > 0 && tHint.length() > 0)
                        {
                            addNewSentence(tSentence, tHint, null);
                            resp.getWriter().write( "\n<br>Thanks! Sentence " + tSentence + " with hint " + tHint + " have been added to the system!" );
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    protected void writeInputForm(HttpServletRequest req, PrintWriter pPrintWriter )
    {
        pPrintWriter.write("\n<form action=\"hangmansentencesbatch\" method=\"get\">\n<br>Sentence : <input type=\"text\" name=\"batchinput\" value=\"\" autofocus />\n<br><input type=\"hidden\" name=\"cmd\" value=\"add\" />\n<br><input type=\"submit\" value=\"Submit\" /></form>");
    }
    
    protected void addNewSentence(String pHangmanSentence, String pHint, String pExtras)
    {
        pHangmanSentence = pHangmanSentence.trim().toLowerCase();
        
        HangmanQuestion tQuestion = new HangmanQuestion( pHangmanSentence, pHint, pExtras );
        
        Objectify tObjectify = ObjectifyService.begin();
        tObjectify.put( tQuestion );
        
        
    }
}
